import java.util.Scanner;

public class Shang {
    public static void main(String[] args) {
        int i = 0;
        final int N = 5;
        float sum = 0;
        double H = 0;
        System.out.print("请输入离散信源概率：    ");
        @SuppressWarnings("resource")
        Scanner in = new Scanner(System.in);
        float p[] = new float[N];
        for (i = 0; i < N; i++) {
            p[i] = in.nextFloat();
        }
        System.out.print("\n");
        System.out.print("P(x):    ");
        for (i = 0; i < N; i++)
            System.out.print(p[i] + "    ");
        System.out.println("\n");
        for (i = 0; i < N; i++)
            sum += p[i];
        System.out.println("离散信源概率和 =  " + sum);
        System.out.println("\n");
        System.out.print("H(x):    ");

        for (i = 0; i < N; i++) {
            H = H + (p[i] * (Math.log(1 / p[i]) / Math.log(2)));
            System.out.print(H + "    ");
        }
        System.out.println("\n");
        System.out.println("一维离散信源熵:	H(x) =   " + H);
    }

    public static double log(double value, double base) // 以base为底，value为真数的对数
    {
        return Math.log(value) / Math.log(base);
    }
}
